package com.ntd.study.reactive;

import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;

/**
 * 响应式编程
 */
public class Rective {
    class MyPub implements Flow.Publisher { // 发布者(生产者)
        @Override
        public void subscribe(Flow.Subscriber subscriber) {
        }
    }

    class MySub implements Flow.Subscriber { // 订阅者(消费者)
        @Override
        public void onSubscribe(Flow.Subscription subscription) {
        }

        @Override
        public void onNext(Object item) {
        }

        @Override
        public void onError(Throwable throwable) {
        }

        @Override
        public void onComplete() {
        }
    }

    class MySubscription implements Flow.Subscription { // 用于发布者与订阅者之间的通信(实现背压：订阅者能够告诉生产者需要多少数据)
        @Override
        public void request(long n) {
        }

        @Override
        public void cancel() {
        }
    }

    static class MyProcesor implements Flow.Processor<Integer, String> { // 处理器
        @Override
        public void subscribe(Flow.Subscriber<? super String> subscriber) {
        }

        @Override
        public void onSubscribe(Flow.Subscription subscription) {
        }

        @Override
        public void onNext(Integer item) {
        }

        @Override
        public void onError(Throwable throwable) {
        }

        @Override
        public void onComplete() {
        }
    }

    // 用于处理发布者 发布消息后，对消息进行处理，再交由消费者消费
    public interface Processor<T, R> extends Flow.Subscriber<T>, Flow.Publisher<R> {
    }

    public static void main(String[] args) {
        // 1. 定义发布者, 发布的数据类型是 Integer - 直接使用jdk自带的SubmissionPublisher
        SubmissionPublisher<Integer> publiser = new SubmissionPublisher();
        // 2. 定义处理器, 对数据进行过滤, 并转换为String类型
        final MyProcesor myProcesor = new MyProcesor();
        publiser.subscribe(myProcesor);
        publiser.submit(123);
    }
}
